package com.lijing.ThreadedBinaryTree;

/**
 * @Description hero node
 * @title: HeroNode
 * @Author LiJing
 * @Date: 2021/2/189:29 下午
 * @Version 1.0
 */
public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    private int leftType;
    private int rightType;

    /**
     * 前序遍历
     */
    public void preOrder(){
        System.out.println(this);
        if (this.left != null){
            this.left.preOrder();
        }
        if (this.right != null){
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder(){
        if (this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.infixOrder();
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder(){
        if (this.left != null){
            this.left.postOrder();
        }
        if(this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    /**
     * 前序遍历查找
     * @param no 待查找的no
     * @return 返回对应no的HeroNode，若没有返回null
     */
    public HeroNode preOrderSearch(int no){
        if (this.no == no){
            return this;
        }
        HeroNode heroNode = null;
        if (this.left != null){
            heroNode = this.left.preOrderSearch(no);
        }
        if (heroNode != null){
            return heroNode;
        }
        if (this.right != null){
            heroNode = this.right.preOrderSearch(no);
        }
        return heroNode;
    }

    /**
     * 中序遍历查找
     * @param no 待查找的no
     * @return 找到返回对应no的HeroNode，没找到返回null
     */
    public HeroNode infixOrderSearch(int no){
        HeroNode heroNode = null;
        if (this.left != null){
            heroNode = this.left.infixOrderSearch(no);
        }
        if (heroNode != null){
            return heroNode;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            heroNode = this.right.infixOrderSearch(no);
        }
        return heroNode;
    }

    /**
     * 后序遍历查找
     * @param no 待查找的no
     * @return 找到返回对应no的HeroNode，没找到返回null
     */
    public HeroNode postOrderSearch(int no){
        HeroNode heroNode = null;
        if (this.left != null){
            heroNode = this.left.postOrderSearch(no);
        }
        if (heroNode != null){
            return heroNode;
        }
        if (this.right != null){
            heroNode = this.right.postOrderSearch(no);
        }
        if (heroNode != null){
            return heroNode;
        }
        if (this.no == no){
            return this;
        }
        return null;

    }

    public HeroNode() {
    }

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}